#Lucas Godshalk
#Python script that constructs an svg file for the grid of holes.
#Can also construct a gcode script for drilling the sheet directly.

import time
import math
output = raw_input("What file format? (svg, gcode, dxf): ")

if output == 'svg':
    distance = 30  #Used when creating svg
    largeradius = 10
    smallradius = 13
elif output == 'gcode':
    distance = 3	#Used when creating gcode
elif output == 'dxf':
    distance = 1.67
    largeradius = 0.7
    smallradius = 0.56

#Proportion is roughy 10 pts to 1 mm.

#Gives the number of holes in the x and y direction.
xnumberofholes = 360
ynumberofholes = 228

h = math.pow(0.75,0.5)*distance

lowcirclearea = 3.14*smallradius**2
highcirclearea = 3.14*largeradius**2

rectarea = distance*h

lowfillingfraction = 1.0 - lowcirclearea / rectarea
highfillingfraction = 1.0 - highcirclearea / rectarea

#Prints the filling fraction of the two parts of the sheet.
print('PVC density filling fraction region 1: ' + str(lowfillingfraction))
print('PVC density filling fraction region 2: ' + str(highfillingfraction))
print('Sheet dimensions: ' + str(xnumberofholes*distance) + ' x ' + str(ynumberofholes*h) + 'mm')

#FOR SVG
if output == 'svg':

    file = open('design.svg', 'w')

    file.write('''<?xml version="1.0" standalone="no"?>
	<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
	"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
	<svg xmlns="http://www.w3.org/2000/svg" version="1.1">''')

    for x in range(xnumberofholes):
        for y in range(ynumberofholes):
            if y % 2 == 1:
                xtra = largeradius + distance/2
            else:
                xtra = largeradius

            ytra = largeradius

            xdist = x*distance + xtra
            ydist = y*h + ytra

            if y < (3*ynumberofholes)/xnumberofholes*x:
                radius = smallradius #higher density
            else:
                radius = largeradius #lower density

            string = '''<circle cx="''' +str(xdist) + '''" cy="''' + str(ydist) + '''" r="''' + str(radius) + '''" stroke="black" stroke-width="1" fill="black" />'''
            file.write(string)


    file.write('''\n</svg>''')

    file.close()

#FOR GCODE
elif output == 'gcode':

    extension = 'nc'

    file = open('sheet.' + extension, 'w')
    file.write('(File Comments) \n')

    #This next write inserts the machine preamble, setting a variety of configurations.
    #In order of codes, sets: contour plane, mm mode, absolute coordinate positioning, feed rate to inches/minute.
    file.write('%')
    file.write('G17 G21 G90 G94 \n')

    file.write('T01')

    #Initial positioning

    file.write('G0 X0. Y0. Z4.0 \n')
    skiplist = []
	
    for x in range(xnumberofholes):
    	for y in range(ynumberofholes):
            if y % 2 == 1:
                xtra = largeradius + distance/2
            else:
                xtra = largeradius

            ytra = largeradius

            xdist = x*distance + xtra
            ydist = y*h + ytra
            #Adds the set of holes to be drilled with a different bit.
            if y > (3*ynumberofholes)/xnumberofholes*x:
                skiplist.append((xdist, ydist))
            else:
                #Positioning string
                gotostring = 'G0 X' + str(xdist) + ' Y' + str(ydist) + '\n'	
                file.write(gotostring)

    		#Hole drilling sequence
                file.write('G0 Z1.0\n')
                file.write('G1 Z0. F40.0\n')
                file.write('G1 Z1.0\n')
                file.write('G0 Z4.0\n')
	
	#Switch to a second tool.
    file.write('T02')
	
	#Fillings in every point on the grid that has the other filling fraction.
    for a in skiplist:
		
        xdist = a[0]
        ydist = a[1]
        gotostring = 'G0 X' + str(xdist) + ' Y' + str(ydist) + '\n'	
        file.write(gotostring)

		#Drill Down
        file.write('G0 Z1.0\n')
        file.write('G1 Z0. F40.0\n')
        file.write('G1 Z1.0\n')
        file.write('G0 Z4.0\n')
	

    file.write('%')

    file.close()

    print('done')
    time.sleep(3)

#FOR DXF
elif output == 'dxf':

    from dxfwrite import DXFEngine as dxf
    drawing = dxf.drawing('sheet.dxf')

    drawing.header['$LUNITS'] = 4 #Sets units to mm (maybe?)
    #drawing.header['$PLIMMIN'] = (0,0)
    #drawing.header['$PLIMMAX'] = (400,400)
    
        
    for x in range(xnumberofholes):
        for y in range(ynumberofholes):
            if y % 2 == 1:
                xtra = distance/2

            else:
                xtra = 0

            ytra = largeradius

            xdist = x*distance + xtra
            ydist = y*h + ytra

            if x > 3*xnumberofholes/8 + 0.5*y:
                 radius = smallradius #higher density
            else:
                radius = largeradius #lower density
            circle = dxf.circle(radius, (xdist, ydist))
            circle['thickness'] = 0.001
            drawing.add(circle)


    drawing.save()


	
